/**
 * Created by Dalyn on 2017/2/28.
 */
import React from "react";
import { hashHistory } from 'react-router';
import Confirm from '../../common/Confirm'
import Relay from "react-relay";
import ListOrderMutation from "./mutation/ListOrderMutation"
import Check from './Check'
import Order_Item from './Order_Item'
import './css/IndentManage.css'
import Util from "../../util/Util"
import Pagination from "./Pagination"
import Reactkey from "../../util/ReactKey"
/*信息列表*/
export default class ListOrder extends React.Component{
    constructor(props){
        super(props);
        this.state={
            orders:[],
            totalresult:0
        };
        this.enterpriseId= sessionStorage.getItem('enterpriseId');
        this.permissionId= sessionStorage.getItem('permissionId');
        this.oddetail=["销售订单:1","采购订单:2"];
        this.oddstatus=["全部:","待发布:0","已发布:1","已签订:3","已完成:4"];
        this.oddstatussell=["全部:","待接收:1","已接收:2","已签订:3","已完成:4"];;
        this.odtail='1';
        this.ods='';
        this.odtobj={1:'销售订单',2:'采购订单'};
        this.odsobj={0:'待发布',1:'已发布',3:'已签订',4:'已完成'};
        this.odsobjsell={1:'待接收',2:'已接收',3:'已签订',4:'已完成'}

    }


    componentWillMount(){
        this.queryorderlist(10,1);
    }
    componentDidMount(){
        this.refs['de'+0].chose();
        this.refs['df'+0].chose();
    }


    queryorderlist(pageSize,pageNum){
        Relay.Store.commitUpdate(
            new ListOrderMutation({
                pageSize: pageSize,
                pageNum: pageNum,
                enId: this.enterpriseId,
                status:this.ods,
                type:this.odtail
            }),
            {
                onFailure: () => {
                    alert('请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.listOrder.listOrder.errorCode;
                    if(errorCode=='0'){
                        let orders = response.listOrder.listOrder.orders
                        let totalResult = response.listOrder.listOrder.totalResult
                        this.setState({
                            orders:orders,
                            totalresult:totalResult
                        });
                    }
                    // console.log(response )
                }
            }
        )
    }

    handleclick(){

    }


    choseoddetail(indexd,value){
        this.odtail = value
        this.oddetail.map((context,index)=>{
            if(indexd!=index){
                this.refs['de'+index].unchose();
            }
        });
        this.queryorderlist(10,1);
        this.refs.pagination.setState({
            text:1
        });
    }

    choseodorderstatus(indexd,value){
        this.ods = value;
        this.oddstatus.map((context,index)=>{
            if(indexd!=index){
                this.refs['df'+index].unchose();
            }
        });
        this.queryorderlist(10,1);
        this.refs.pagination.setState({
            text:1
        });
    }



    addorderdetail(){
        return this.oddetail.map((con,index)=>{
            return(
                <Order_Item choseodstate={this.choseoddetail.bind(this)} key={index} ref={'de'+index} index={index} con={con}/>
            )

        })
    }

    addorderstatus(){
        if(this.odtail==1){
            return this.oddstatussell.map((con,index)=>{
                return(
                    <Order_Item choseodstate={this.choseodorderstatus.bind(this)} key={index+'_'} ref={'df'+index} index={index} con={con}/>
                )

            })
        }else{
            return this.oddstatus.map((con,index)=>{
                return(
                    <Order_Item choseodstate={this.choseodorderstatus.bind(this)} key={index+'_'} ref={'df'+index} index={index} con={con}/>
                )

            })
        }

    }


    check(){
        this.refs.check.setState({
            visible : true
        })
    }
    addDepartmenlist(){
        return this.state.orders.map((context,index)=>{
            let date = Util.dateFormat(context.createDate);
            let ods
            let odt = this.odtobj[context.orderType];

            if(this.odtail==1){
                ods = this.odsobjsell[context.status];
            }else{
                ods = this.odsobj[context.status];
            }

            return (
                    <tr key={index+"-"} style={{height:48,borderBottom:"1px dashed #e4e4e4"}}>
                        <th >{date}</th>
                        <th>{context.oid}</th>
                        <th>{context.partA}</th>
                        <th>{odt}</th>
                        <th>{ods}</th>
                        <th style={{color:"#66b0c3",cursor:"pointer"}} onClick={this.check.bind(this)}>操作</th>
                    </tr>
            );
        });
    }

    stroke(){

    }

    status(){
        return this.oArr.map((item,index)=>{
            return (
                <span style={{width:80,height:27,borderRadius:"8px",background:"#ccc",display:"block"}} onClick={this.stroke.bind(this)}>{item}</span>
            )
        })
    }

    createorder(){
        hashHistory.push('/createorder')
    }

    onSure(size,num){
        this.queryorderlist(size,num);
    }

    render(){
        return (
            <div className="order_box" style={{width:1233,minHeight:475,background:"#f5f5f5",paddingTop:"20px"}}>
                <p style={{height:27}} >
                    <span style={{display:"block",float:"left",marginLeft:"30px",height:27,lineHeight:"27px"}}>订单详情：</span>
                    {
                        this.addorderdetail()
                    }

                </p>
                <p style={{height:27,marginTop:"20px"}} >
                    <span style={{display:"block",float:"left",marginLeft:"30px",height:27,lineHeight:"27px"}}>订单状态：</span>
                    {
                        this.addorderstatus()
                    }
                </p>
                <p style={{height:27,marginTop:"30px"}} >符合条件的订单共有<span>{this.state.num}</span>个：
                    <Confirm touch={this.createorder.bind(this)} tcontext="新建订单" style={{marginLeft:"930px",width:130,height:30,color:"#fff"}}/>
                </p>
                <div style={{width:1193,height:"592px",background:"#fff",marginTop:"20px",padding:"38px 19px 0 19px",position:"relative"}}>
                    <table style={{width:1193,borderCollapse:"collapse"}}>
                            <thead>
                            <tr style={{height:"48px",padding:"0px",background:"#f2f2f2"}}>
                                <th style={{height:48,padding:"0px"}}>发布时间</th>
                                <th style={{height:48,padding:"0px"}}>订单编号</th>
                                <th style={{height:48,padding:"0px"}}>甲方</th>
                                <th style={{height:48,padding:"0px"}}>商品类型</th>
                                <th style={{height:48,padding:"0px"}}>订单状态</th>
                                <th style={{height:48,padding:"0px"}}>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                                {
                                    this.addDepartmenlist()
                                }
                            </tbody>
                    </table>
                    <Check ref={"check"}/>
                    <Pagination eachSize={10} dataSize={this.state.totalresult} onSure={this.onSure.bind(this)} ref={'pagination'}/>
                </div>
            </div>

        )
    }
}





